<?php

namespace App\Http\Controllers\Client;

use App\Services\Tbk\PddService;
use App\Utils\ApiResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PddController extends Controller
{
    private $pid = "38401894_272927176";

    public function searchAuth()
    {
        $pddService = new PddService();
        $res = $pddService->searchAuth($this->pid);
        if ($res['authority_query_response']['bind'] == 0) {
            echo '用户未授权，请前往授权、pdd_set_auth';
        }
        echo '用户已授权';
    }

    public function setAuth()
    {
        $pddService = new PddService();
        $res = $pddService->setAuth($this->pid);
        $link = $res['rp_promotion_url_generate_response']['url_list'][0]['mobile_url'];
        echo '<a href=' . $link . '>授权链接</a>';
    }

    public function searchGoodsInfo(Request $request)
    {
        //$url = $request->get('url','');
        $url = "https://mobile.yangkeduo.com/goods2.html?_wvx=10&refer_share_uin=FWKSD5NN6YFW4VSDNUB7BX5V7A_GEXDA&_oak_share_time=1692847010&share_uin=FWKSD5NN6YFW4VSDNUB7BX5V7A_GEXDA&page_from=26&_wv=41729&refer_share_channel=copy_link&refer_share_id=c7MZv9wn7GHi2PudWbUQ51rDt9PfXb9q&_oak_share_snapshot_num=5900&pxq_secret_key=DEZRKK5M7RB5QKWDB5ZP6G2IKPFLF25LTU7A6RRHHQXON4C4HDQQ&goods_id=469441395501";
        $pddService = new PddService();
        $goodsInfo = $pddService->searchGoodsInfoByKeyword($url, $this->pid);
        if (empty($goodsInfo['goods_list'])) {
            echo "优惠信息查询失败";
            die;
        }
        dump($goodsInfo);
        $goodsSign = $goodsInfo['goods_search_response']['goods_list'][0]['goods_sign'];
        dd($goodsSign);
    }

    public function updateLink(Request $request)
    {
        //$goodsSign = $request->get('goods_sign', '');
        $pddService = new PddService();
        $goodsSign = "E9X2zyatxbljExiBwf_cxjObcKNJhMGRyw_JPr2iik6U";
        $goodsInfo = $pddService->updateLink($goodsSign, $this->pid);
        ApiResponse::success([
            'url' => $goodsInfo['goods_promotion_url_generate_response']['goods_promotion_url_list'][0]['mobile_url']
        ]);
    }
}
